package com.ax.business.common.service.impl;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ax.base.system.domain.SysUserDO;
import com.ax.business.common.service.AssociatorCenterService;
import com.ax.business.common.util.PageUtils;
import com.ax.business.common.util.Query;
import com.ax.business.prescription.dao.BaseAssociatorMapper;
import com.ax.business.prescription.domain.BaseAssociator;
import com.google.gson.Gson;

@Service("AssociatorCenterService")
public class AssociatorCenterServiceImpl implements AssociatorCenterService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Autowired
	BaseAssociatorMapper baseAssociatorMapper;
	/**
	 * 看医生开处方(模块:生成单号)
	 * 
	 * @param businessCode
	 *           
	 * @return String
	 */
	@Override
	public void insertAssociatorSer(BaseAssociator baseAssociator,HttpServletRequest request) {
		Object sessionId = request.getSession().getAttribute("sessionId");
		Gson gson = new Gson();
		SysUserDO user = null;
		String userJson = redisTemplate.opsForValue().get(sessionId);
		if(StringUtils.isNotEmpty(userJson)){
		user =  gson.fromJson(userJson, SysUserDO.class);
		}
		baseAssociator.setRegistrarId(user.getUserId());
		baseAssociator.setRegistrarName(user.getName());
		baseAssociatorMapper.insertSelective(baseAssociator);
	}
	
	/**
	 * 查询会员(模块:会员中心)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public PageUtils queryAssociatorSer(Map<String, Object> params) {
		Query query = new Query(params);
		List<BaseAssociator> baseAssociators = baseAssociatorMapper.queryAssociatorMapper(query);
		Integer count = baseAssociatorMapper.count(query);
		PageUtils pageUtil = new PageUtils(baseAssociators,count);
		return pageUtil;
	}

	/**
	 * 修改会员(模块:会员中心)
	 * 
	 * @param BaseAssociator baseAssociator
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateAssociatorSer(BaseAssociator baseAssociator) {
		baseAssociatorMapper.updateByPrimaryKeySelective(baseAssociator);
	}
}
